package com.yanqu.road.server.manager.busyfarming;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.pb.activity.BusyFarmingProto;
import com.yanqu.road.server.TempMgr;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossBusyFarmingMgr extends TempMgr {

    private static Logger logger = LogManager.getLogger(CrossBusyFarmingMgr.class.getName());

    //activityId
    private static Map<Integer, CrossBusyFarming> busyFarmingMap = new ConcurrentHashMap<>();

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    /**
     * 更新玩家果园数据
     */
    public static void updateUserData(int activityId, long serverId, List<BusyFarmingProto.BusyFarmingUserDataTemp> dataTempList) {
        CrossBusyFarming crossBusyFarming = getActivity(activityId);
        if(crossBusyFarming != null) {
            crossBusyFarming.upDateUserData(serverId, dataTempList);
        }
    }

    /**
     * 随机三个可偷取果园
     */
    public static void searchStealList(long serverId, int activityId, String unionUid, long playerId, List<Long> friend, int grade, int gradeRange, List<Long> stolen) {
        CrossBusyFarming crossBusyFarming = getActivity(activityId);
        if(crossBusyFarming != null) {
            crossBusyFarming.searchStealList(serverId, unionUid, playerId, friend, grade, gradeRange, stolen);
        }
    }

    /**
     * 查看可偷取果园详情
     */
    public static void searchStealDetail(long serverId, long playerId, int activityId, long userId, long userServerId) {
        CrossBusyFarming crossBusyFarming = getActivity(activityId);
        if(crossBusyFarming != null) {
            crossBusyFarming.searchStealDetail(serverId, playerId, userId, userServerId);
        }
    }

    /**
     * 预留接口
     */
    public static void repair(int activityId) {
        CrossBusyFarming activity = getActivity(activityId);
        if (activity != null) {
            activity.repair(activityId);
        }
    }

    /**
     * 获取活动
     */
    public static CrossBusyFarming getActivity(int activityId) {
        if (busyFarmingMap.get(activityId) == null) {
            synchronized (busyFarmingMap) {
                if (busyFarmingMap.get(activityId) == null) {
                    // 读配置 获取活动信息 保证有活动信息
                    ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activityId);
                    if (activityInfo == null) {
                        return null;
                    }
                    CrossBusyFarming activity = new CrossBusyFarming(activityInfo);
                    activity.initConfig();
                    busyFarmingMap.put(activityId, activity);
                }
            }
        }
        return busyFarmingMap.get(activityId);
    }

    /**
     * 商会 果园list
     */
    public static BusyFarmingProto.BusyFarmingFriendHomeListRespMsg.Builder friendHomeList(long serverId, long playerId, int activityId, int type,
                List<BusyFarmingProto.BusyFarmingFriendId> userList, boolean myCanWatering, int wateringLimit) {
        CrossBusyFarming activity = getActivity(activityId);
        if (activity != null) {
            return activity.friendHomeList(serverId, playerId, type, userList, myCanWatering, wateringLimit);
        }

        BusyFarmingProto.BusyFarmingFriendHomeListRespMsg.Builder respMsg = BusyFarmingProto.BusyFarmingFriendHomeListRespMsg.newBuilder();
        respMsg.setType(type);
        return respMsg;
    }

}
